package com.fpf.config;

import com.fpf.handler.ChatWebSocketHandler;
import com.fpf.service.IWindowTextService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
@EnableAsync
public class WebSocketConfig implements WebSocketConfigurer {

    //通过构造函数注入
    private final IWindowTextService windowTextService;

    public WebSocketConfig(IWindowTextService windowTextService) {
        this.windowTextService = windowTextService;
    }


    //注册WebSocket处理器，chatWebSocketHandler()为处理器
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new ChatWebSocketHandler(windowTextService), "/ws/chat")
                .setAllowedOrigins("*");
    }
}